/**
 * 数字过渡变化
 */
import formatNum from './formatNum';
import { computed, ref } from 'vue';
const num = ref(0);
let numTo = 0;
let timer = -1;
function autoRenderNumber(time = 0, scale = 0) {
  const offset = numTo - num.value;
  if (offset < .5) {
    num.value = numTo;
  } else {
    num.value += (numTo - num.value) * scale / 30;
  }
  if (numTo !== num.value) {
    timer = requestAnimationFrame((newTime) => {
      autoRenderNumber(newTime, time ? (newTime - time) / 16.7 : 1);
    });
  }
}
/* 设置数字 */
export function setNumber(n: number, focus = false) {
  numTo = n;
  if (focus) {
    num.value = n;
  }
  if (numTo !== num.value) {
    cancelAnimationFrame(timer);
    autoRenderNumber();
  }
}
/* 获取format之后的数字字符串 */
export const getNumber = computed(() => formatNum(Math.round(num.value)));
